#!/usr/bin/env python2.4
__license__ = "GNU Lesser General Public License, Ver.2"
__author__ = "Grigory Bakunov <bobuk@justos.org>"

import web
import markdown
import os
import wmtools

from wmauth import *
from config import *
from wmconfig import Config


class rss:
    # Look at this shaman dances wit 'DATE_FORMAT'.
    # It's because I too lazy to read documentation about 'ARPA' date format
    def GET(self):
        pods = web.query(
            "select id," +
            "subject, shownote, rawnote," +
            "DATE_FORMAT(created, '%%a, %%d %%b %%Y %%H:%%i:%%s " + \
            timeZone + \
            "') as created," +
            "url, commnum from pods order by created desc limit 10")
        lastupdate = web.query(
            "select DATE_FORMAT(max(created)," + \
            " '%%a, %%d %%b %%Y %%H:%%i:%%s " + \
            timeZone + \
            "') as lastupdate from pods")[0].lastupdate
        web.header('Content-Type', 'text/xml')
        return web.render('rss.xml')

class podcast:
    def GET(self, idnum):
        (user, edit) = getAccess(web.cookies())
        num = int(idnum)
        pod = web.query("select * from pods where id = %d" % num)[0]
        comments = web.query("select * from comments where podsid = %d order by created asc" % num)
        return web.render('podcast.read.html')

    def POST(self, idnum):
        (user, edit) = getAccess(web.cookies())
        if not edit:
            wmtools.raiseError(2)
        num = int(idnum)
        inp = web.input()
        n = web.update('pods', num,
                       subject   = inp.subject,
                       rawnote   = inp.shownote,
                       shownote  = markdown.markdown(inp.shownote),
                       url       = inp.url)
        return web.seeother(Config().cfg['c_asbpath'] +'/pod/'+str(num))

class podcastDel:
    def GET(self, idnum):
        (user, edit) = getAccess(web.cookies())
        if not edit:
            wmtools.raiseError(2)
        num = int(idnum)
        web.delete('pods', num)
        web.delete('comments', 'podsid = %d' % num)
        return web.seeother(Config().cfg['c_abspath'] + '/podcast')

class podcastNew:
    def POST(self):
        (user, edit) = getAccess(web.cookies())
        if not edit:
            wmtools.raiseError(2)
        inp = web.input()
        n = web.insert('pods',
                       subject   = inp.subject,
                       rawnote   = inp.shownote,
                       shownote  = markdown.markdown(inp.shownote),
                       url       = inp.url)
        return web.seeother(Config().cfg['c_abspath'] + '/pod/' + str(n))

class podcastComment:
    def POST(self, idnum):
        (user, edit) = getAccess(web.cookies())
        inp = web.input()
        num = int(idnum)
        n = web.insert('comments',
                       podsid    = num,
                       author    = inp.author,
                       comment   = markdown.markdown(inp.comment))
        web.query('update pods set commnum=(select count(*) from comments where podsid=%d) where id = %d' %
                  (num, num))
        return web.seeother(Config().cfg['c_abspath'] + '/pod/' + str(num))

class podcastCommentDel:
    def GET(self, idnum, commnum):
        (user, edit) = getAccess(web.cookies())
        if not edit:
            wmtools.raiseError(2)
        inp = web.input()
        num = int(idnum)
        cmnum = int(commnum)
        web.delete('comments', cmnum)
        web.query('update pods set commnum=(select count(*) from comments where podsid=%d) where id = %d' %
                  (num, num))
        return web.seeother(Config().cfg['c_abspath'] + '/pod/' + str(num))
    
class podcasts:
    def GET(self):
        (user, edit) = getAccess(web.cookies())
        pods = web.query("select * from pods order by created desc limit 10")
        return web.render('podcast.html')
